"use strict";
cc._RF.push(module, 'b82eeqhikNAJ4yXszS6ARDs', 'LoginController');
// Script/LoginController.js

"use strict";

var _PlatformUtil = require("./PlatformUtil");

var _PlatformUtil2 = _interopRequireDefault(_PlatformUtil);

var _XmlHttpTool = require("./XmlHttpTool");

var _XmlHttpTool2 = _interopRequireDefault(_XmlHttpTool);

var _StartGameView = require("./StartGameView");

var _StartGameView2 = _interopRequireDefault(_StartGameView);

var _xor = require("./Tool/xor");

var _xor2 = _interopRequireDefault(_xor);

var _allVariable = require("./allVariable");

var _allVariable2 = _interopRequireDefault(_allVariable);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

cc.Class({
  extends: cc.Component,

  properties: {
    loginBtn: cc.Button,
    infoLabel: {
      default: null,
      type: cc.Label
    },
    startGamejs: _StartGameView2.default,
    userInfoDic: "",
    showHubProjess: cc.Node,
    SvprogressPrefab: cc.Prefab,
    logonode: cc.Node,
    guestBtn: cc.Button,
    userTokenCode: ""
  },

  onLoad: function onLoad() {

    var changebig2 = cc.scaleTo(0.2, 1.2);

    var finish = cc.callFunc(function () {

      var changesmail2 = cc.scaleTo(0.1, 1.0);
      this.logonode.runAction(changesmail2);
    }, this);

    var seq = cc.sequence(changebig2, finish);

    this.logonode.runAction(seq);
    console.log("token==", _allVariable2.default.userToken);
    if (_allVariable2.default.userToken) {
      console.log("facebook已经登录过了", _allVariable2.default.userToken);
      _PlatformUtil2.default.init();
      _PlatformUtil2.default.platform.init(this);
      _PlatformUtil2.default.platform.params["token"] = _allVariable2.default.userToken;
      this.startGamejs.getUserInfoHttp();
      this.node.active = false;
    } else {

      console.log("facebook第一次登录");
      var playerID = FBInstant.player.getID();
      console.log("准备登录用户ID==", playerID);
      _PlatformUtil2.default.init();
      _PlatformUtil2.default.platform.init(this);
      this.loginGame(playerID);
    }
  },


  loginGameSence: function loginGameSence() {

    var playerID = FBInstant.player.getID();
    console.log("准备登录用户ID==", playerID);
    _PlatformUtil2.default.init();
    _PlatformUtil2.default.platform.init(this);
    this.loginGame(playerID);
  },

  loginGame: function loginGame(userIdString) {

    // if(cc.isValid(this.showHubProjess)){
    //     this.showHubProjess.destroy();
    //   }
    //   this.showHubProjess = cc.instantiate(this.SvprogressPrefab);
    //   this.node.addChild(this.showHubProjess);
    //   this.showHubProjess.getComponent('SVProgressView').init("Logging in..Please Waiting")

    // let action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(10), cc.fadeOut(1));
    // this.showHubProjess.runAction(action);

    var resultstr = _xor2.default.encryptionData(userIdString, _PlatformUtil2.default.platform.params);

    console.log("code加密", resultstr);
    var that = this;
    _XmlHttpTool2.default.httpPost("/quick_users/wxregister", { "regtype": 1, "code": resultstr }, function (res) {

      console.log("获取token", res["token"]);
      var tokenCode = res["token"];

      _PlatformUtil2.default.platform.setStorage({ key: "tokenCode", value: res["token"] });
      var tokenstring = _PlatformUtil2.default.platform.getStorage("tokenCode");
      console.log("本地token", tokenstring);

      _allVariable2.default.userToken = tokenstring;
      console.log("全新字典1", JSON.stringify(_PlatformUtil2.default.platform.params));
      _PlatformUtil2.default.platform.params["token"] = res["token"];
      console.log("全新字典2", JSON.stringify(_PlatformUtil2.default.platform.params));
      that.save_UserInfo(userIdString);
    }, function (failres) {

      console.log("时间超时1", JSON.stringify(failres));
      if (cc.isValid(that.showHubProjess)) {
        that.showHubProjess.destroy();
      }
      that.showHubProjess = cc.instantiate(that.SvprogressPrefab);
      that.node.addChild(that.showHubProjess);
      that.showHubProjess.getComponent('SVProgressView').init("登录超时,请重新登录");

      var action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(5), cc.fadeOut(1));
      that.showHubProjess.runAction(action);
      that.loginBtn.active = true;
    }, function () {
      console.log("时间超时2");
      if (cc.isValid(that.showHubProjess)) {
        that.showHubProjess.destroy();
      }
      that.showHubProjess = cc.instantiate(that.SvprogressPrefab);
      that.node.addChild(that.showHubProjess);
      that.showHubProjess.getComponent('SVProgressView').init("登录超时,请重新登录");

      var action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(5), cc.fadeOut(1));
      that.showHubProjess.runAction(action);
      that.loginBtn.active = true;
    });
  },

  save_UserInfo: function save_UserInfo(userIdString) {

    var that = this;

    var playerName = FBInstant.player.getName();
    var playPhotoUrl = FBInstant.player.getPhoto();
    _XmlHttpTool2.default.httpPost("/users/save_user_info", { "avatar": playPhotoUrl, "name": playerName, "gender": "0" }, function (res) {

      console.log("存储用户信息", JSON.stringify(res));
      that.startGamejs.getUserInfoHttp();
      that.node.active = false;
    }, function (failres) {
      console.log("时间超时3");
      if (cc.isValid(that.showHubProjess)) {
        that.showHubProjess.destroy();
      }
      that.showHubProjess = cc.instantiate(that.SvprogressPrefab);
      that.node.addChild(that.showHubProjess);
      that.showHubProjess.getComponent('SVProgressView').init("登录超时,请重新登录");
      that.loginBtn.active = true;
      var action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(5), cc.fadeOut(1));
      that.showHubProjess.runAction(action);
    }, function () {
      console.log("时间超时4");
      if (cc.isValid(that.showHubProjess)) {
        that.showHubProjess.destroy();
      }
      that.showHubProjess = cc.instantiate(that.SvprogressPrefab);
      that.node.addChild(that.showHubProjess);
      that.showHubProjess.getComponent('SVProgressView').init("登录超时,请重新登录");
      that.loginBtn.active = true;

      var action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(5), cc.fadeOut(1));
      that.showHubProjess.runAction(action);
    });
  },

  start: function start() {
    console.log("fb小游戏初始化");
  }
});

cc._RF.pop();